package messages;

import java.io.IOException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import connection.exception.ConnectionResetException;

import utils.Serialization;
import utils.XmlSerializationUtils;

public class NonceToSignRequestMessage implements Convertible {

    private static final long serialVersionUID = 1L;

    public String _firstName;
    
    public String _lastName;
    
    public String _competence;
    
    public NonceToSignRequestMessage(){}
    
    public NonceToSignRequestMessage(String firstName, String lastName, String competence) {
        _firstName = firstName;
        _lastName = lastName;
        _competence = competence;
    }
    
    @Override
    public Element convertToCanonic(Document doc) {
        
        String className = getClass().getSimpleName();
        
        Element el = doc.createElement(className);
        
        if (_firstName != null) {
            Element childEl = doc.createElement("FirstName");
            childEl.setTextContent(_firstName);
            el.appendChild(childEl);

        }
        
        if (_lastName != null) {
            Element childEl = doc.createElement("LastName");
            childEl.setTextContent(_lastName);
            el.appendChild(childEl);

        }
        
        if (_competence != null) {
            Element childEl = doc.createElement("Competence");
            childEl.setTextContent(_competence);
            el.appendChild(childEl);

        }

        return el;
    } 


    @Override
    public Object convertFromCanonic(Element el) {
        this._firstName = XmlSerializationUtils.getTextValue(el, "FirstName");
        this._lastName = XmlSerializationUtils.getTextValue(el, "LastName");
        this._competence = XmlSerializationUtils.getTextValue(el, "Competence");
        return this;
    }
    
    public static void main(String[] args) throws ConnectionResetException, IOException{

        NonceToSignRequestMessage gen = new NonceToSignRequestMessage("João", "Lima", "Chefe");

        byte[] serialized = Serialization.Serialize(gen);

        Object newGen = Serialization.Deserialize(serialized);

        newGen.toString();

        System.in.read();
    }
}
